package com.runen.wnhz.runen.ui.activity.mine;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.runen.wnhz.runen.R;
import com.runen.wnhz.runen.common.aspectj.CheckNet;
import com.runen.wnhz.runen.common.utils.ACacheUtlis;
import com.runen.wnhz.runen.common.utils.RecycleViewDivider;
import com.runen.wnhz.runen.common.utils.StatusBarCompatUtils;
import com.runen.wnhz.runen.common.utils.ToastUtil;
import com.runen.wnhz.runen.data.entity.MyOrderListBean;
import com.runen.wnhz.runen.data.entity.UserBean;
import com.runen.wnhz.runen.di.component.ApplicationComponent;
import com.runen.wnhz.runen.di.component.DaggerMyOrderComponent;
import com.runen.wnhz.runen.di.module.MyOrderModule;
import com.runen.wnhz.runen.presenter.Contart.MyOrderContract;
import com.runen.wnhz.runen.presenter.iPresenter.IMyOrderPresenter;
import com.runen.wnhz.runen.ui.activity.BaseActivity;
import com.runen.wnhz.runen.ui.adapter.puadapter.MyOrderAdapter;
import com.runen.wnhz.runen.ui.adapter.puadapter.OrderFragmentAdapter;
import com.runen.wnhz.runen.widget.TitleBuilder;
import com.scwang.smartrefresh.layout.util.DensityUtil;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.ViewPagerHelper;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.ColorTransitionPagerTitleView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2018-04-11.
 */

public class MyOrderActivity extends BaseActivity<IMyOrderPresenter>
        implements MyOrderContract.View,
        BaseQuickAdapter.RequestLoadMoreListener {

    @BindView(R.id.magic_indicator)
    MagicIndicator magicIndicator;
    @BindView(R.id.view_orderLabel)
    ViewPager viewOrderLabel;

    private UserBean mUser;
    private int mPage = 0;
    private String mOrderId;

    @Override
    protected int getLayoutView() {
        return R.layout.my_order_activity_layout;
    }

    @Override
    protected void onTitleClickListen(TitleBuilder.TitleButton clicked) {
        switch (clicked) {
            case LEFT:
                finish();
                break;
        }
    }

    @Override
    protected void initStart() {
        this.mUser = ACacheUtlis.getInstance().getReqeustUser(this);

        StatusBarCompatUtils.getInstance().TranslucentStatusBar(this, R.color.title_green);
        initData();
        mContext = MyOrderActivity.this;
    }


    @Override
    public void initToolBar(TitleBuilder mTitleBuilder) {
        mTitleBuilder.setMiddleTitleText("我的订单")
                .setLeftDrawable(R.mipmap.icon_back)
                .seTitleBgRes(R.mipmap.bg_title)
                .setMiddleTitleTextColor(ContextCompat.getColor(this, R.color.text_white));
    }

    @Override
    public void setupAcitivtyComponent(ApplicationComponent appComponent) {
        DaggerMyOrderComponent.builder().applicationComponent(appComponent)
                .myOrderModule(new MyOrderModule(this))
                .build().initject(this);
    }

    @Override
    public void onLoadMoreRequested() {

    }


    @Override
    public String getToken() {
        return mUser.getToken();
    }

    @Override
    public String getPage() {
        return mPage + "";
    }

    @Override
    public String getOrderId() {
        return mOrderId;
    }

    @Override
    public void onTokenError(String msg) {
    }

    @Override
    public void onSuccess(MyOrderListBean myOrderListBean) {
    }

    /**
     * 订单取消成功
     */
    @Override
    public void onSuccess() {

    }

    @Override
    public void onError(String msg) {

    }

    private void initData() {
        final String[] userOrderLabel = getResources().getStringArray(R.array.user_order_label);
        CommonNavigator commonNavigator = new CommonNavigator(this);
        commonNavigator.setAdapter(new CommonNavigatorAdapter() {

            @Override
            public int getCount() {
                return userOrderLabel.length;
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                ColorTransitionPagerTitleView colorTransitionPagerTitleView = new ColorTransitionPagerTitleView(context);
                colorTransitionPagerTitleView.setNormalColor(Color.GRAY);
                colorTransitionPagerTitleView.setSelectedColor(getResources().getColor(R.color.title_green));
                colorTransitionPagerTitleView.setText(userOrderLabel[index]);
                colorTransitionPagerTitleView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        viewOrderLabel.setCurrentItem(index);
                    }
                });
                return colorTransitionPagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                LinePagerIndicator indicator = new LinePagerIndicator(context);
                indicator.setMode(LinePagerIndicator.MODE_EXACTLY);
                indicator.setColors(Color.parseColor("#02BF65"));//设置导航栏被选中时的下划线颜色
                indicator.setLineWidth(DensityUtil.dp2px( 50f));//设置导航栏的某一项被选中时的下划线的宽度
                return indicator;
            }
        });
        magicIndicator.setNavigator(commonNavigator);
        viewOrderLabel.setOffscreenPageLimit(6);
        viewOrderLabel.setAdapter(new OrderFragmentAdapter(getSupportFragmentManager(),6));
        ViewPagerHelper.bind(magicIndicator,viewOrderLabel);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
    }
}
